package com.zlll.winner.mapper.bbs;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zlll.winner.business.model.bbs.BbsPost;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zlll.winner.business.vo.bbs.BbsPostVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 论坛频道帖子表 Mapper 接口
 * </p>
 *
 * @author Walert
 * @since 2019-09-12
 */
public interface BbsPostMapper extends BaseMapper<BbsPost> {

    @Select("select " +
            "(select if(count(tt.id)>0,1,0) " +
            "   from bbs_user_action_log tt " +
            "       where tt.user_id = ${userId} " +
            "         and tt.biz_id = t.id " +
            "         and tt.biz_type = 1 " +
            "         and tt.action_type = 1 " +
            "         and tt.is_del = 0) as is_like, "+
            "GROUP_CONCAT(distinct m1.media) as files" +
            ",m2.phone as userPhone" +
            ",GROUP_CONCAT(distinct m4.name) as  userType" +
            ",GROUP_CONCAT(distinct m4.id) as  userTypeIds" +
            ",m5.subject as  tagName" +
            ",t.* " +
            "from bbs_post t " +
            "left join bbs_post_media m1 on (t.id = m1.post_id) " +
            "left join lht_user m2 on (t.user_id = m2.id ) " +
            "left join lht_user_identity m3 on (m2.id = m3.user_id and m3.is_del = 0) " +
            "left join lht_user_identity_type m4 on (m3.identity_id = m4.id and m4.is_del = 0) " +
            "left join bbs_subject m5 on (t.subject_id = m5.id and m5.is_del = 0) " +
            "${ew.customSqlSegment} " +
            "group by t.id " +
            "order by t.create_time desc,t.issue_time desc ")
    IPage<BbsPostVo> selectPageByCustomer(IPage<BbsPostVo> page, @Param("ew") Wrapper<BbsPostVo> queryWrapper,@Param("userId") Long userId);



    @Select("select " +
            "(select if(count(tt.id)>0,1,0) " +
            "   from bbs_user_action_log tt " +
            "       where tt.user_id = ${userId} " +
            "         and tt.biz_id = t.id " +
            "         and tt.biz_type = 1 " +
            "         and tt.action_type = 1 " +
            "         and tt.is_del = 0) as is_like, "+
            "GROUP_CONCAT(distinct m1.media) as files" +
            ",m2.phone as userPhone" +
            ",GROUP_CONCAT(distinct m4.name) as  userType" +
            ",GROUP_CONCAT(distinct m4.id) as  userTypeIds" +
            ",m5.subject as  tagName" +
            ",t.* " +
            "from bbs_post t " +
            "left join bbs_post_media m1 on (t.id = m1.post_id) " +
            "left join lht_user m2 on (t.user_id = m2.id ) " +
            "left join lht_user_identity m3 on (m2.id = m3.user_id and m3.is_del = 0) " +
            "left join lht_user_identity_type m4 on (m3.identity_id = m4.id and m4.is_del = 0) " +
            "left join bbs_subject m5 on (t.subject_id = m5.id and m5.is_del = 0) " +
            "where t.id = ${postId} and t.is_del = 0 " +
            "group by t.id")
    BbsPostVo getDetailsById(@Param("postId") Long postId,@Param("userId") Long userId);

    @Select("select " +
            "(select if(count(tt.id)>0,1,0) " +
            "   from bbs_user_action_log tt " +
            "       where tt.user_id = ${userId} " +
            "         and tt.biz_id = t.id " +
            "         and tt.biz_type = 1 " +
            "         and tt.action_type = 1 " +
            "         and tt.is_del = 0) as is_like, "+
            "GROUP_CONCAT(distinct m1.media) as files" +
            ",m2.phone as userPhone" +
            ",GROUP_CONCAT(distinct m4.name) as  userType" +
            ",GROUP_CONCAT(distinct m4.id) as  userTypeIds" +
            ",m5.subject as  tagName" +
            ",t.* " +
            "from bbs_post t " +
            "left join bbs_post_media m1 on (t.id = m1.post_id) " +
            "left join lht_user m2 on (t.user_id = m2.id ) " +
            "left join lht_user_identity m3 on (m2.id = m3.user_id and m3.is_del = 0) " +
            "left join lht_user_identity_type m4 on (m3.identity_id = m4.id and m4.is_del = 0) " +
            "left join bbs_subject m5 on (t.subject_id = m5.id and m5.is_del = 0) " +
            "${ew.customSqlSegment} " +
            "group by t.id " +
            "order by t.create_time desc,t.issue_time desc ")
    List<BbsPostVo> getDetailsList(@Param("ew") Wrapper<BbsPostVo> queryWrapper,@Param("userId") Long userId);


    Integer listPostById(@Param("postId") Long postId,@Param("updateTime") Long updateTime, @Param("version") Long version);

}
